package com.example.boot.service.ifelse.factory;

import com.example.boot.service.ifelse.EatLunch;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class StrategyFactoryAware implements ApplicationContextAware {

    private static Map<String, EatLunch> eatLunchMap = new ConcurrentHashMap<>();

    public static EatLunch getLunch(String week) {
        return eatLunchMap.get(week);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        Map<String, EatLunch> ofType = applicationContext.getBeansOfType(EatLunch.class);

        ofType.forEach((key, value) -> eatLunchMap.put(value.getWeek(), value));
    }
}
